import axios from 'axios'
import { AxiosInstance } from 'axios'
import { EhRequestConfig, EhRequestInterceptors } from './typeAxios'
import { ElLoading } from 'element-plus'
const DEFAULT_LOADING = true
class EhRequest {
  instance: AxiosInstance
  interceptors?: EhRequestInterceptors
  showLoading: boolean
  loading?: any
  constructor(config: EhRequestConfig) {
    //创建axios的实例
    this.instance = axios.create(config)
    this.interceptors = config.interceptors
    //如果没有传showLoading的话默认为true
    this.showLoading = config.showLoading ?? DEFAULT_LOADING
    //使用拦截器  第一种：从config中拿到的
    //请求拦截
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )
    //响应拦截
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
    //第二种：添加所有实例都有的拦截器 本身就添加的针对于所有的实例
    this.instance.interceptors.request.use(
      (config) => {
        if (this.showLoading) {
          this.loading = ElLoading.service({
            text: '加载中...',
            lock: true,
            background: 'rgba(0,0,0,0.2)',
            body: true
            // spinner: 'el-icon-loading',
          })
        }
        return config
      },
      (err) => {
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        const data = res.data
        this.loading?.close()
        // if (data.returnCode === '-1001') {
        //   console.log('请求失败')
        // } else {
        return data
        // }
      },
      (err) => {
        //判断不同的httpErrorCode显示不同的错误信息
        if (err.response.status === 404) {
          console.log('404的错误')
        }
        return err
      }
    )
  }
  request<T>(config: EhRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.showLoading === false) {
        this.showLoading = config.showLoading
      }
      //单独请求的拦截器
      if (config.interceptors?.requestInterceptor) {
        //如果请求中有拦截器，对传入的数据进行拦截后返回
        config = config.interceptors.requestInterceptor(config)
      }
      this.instance
        .request<any, T, any>(config)
        .then((res) => {
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res)
          }
          resolve(res)
          //将showLoading重新设置为true，这样不会影响下一个请求
          this.showLoading = DEFAULT_LOADING
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING
          reject(err)
        })
    })
  }
  get<T = any>(config: EhRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  post<T = any>(config: EhRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  delete<T = any>(config: EhRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  patch<T = any>(config: EhRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}
export default EhRequest
